package com.example.xml_java

import android.annotation.SuppressLint
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.ViewGroup
import android.widget.GridLayout
import android.widget.ImageView

class MainActivity : AppCompatActivity() {


    //这里需要注意的是放在mipmap中的图片不要以纯数字开头,图片不能太大，否则会报错
    var images:List<Int> = listOf(R.mipmap.img01,R.mipmap.img02,R.mipmap.img03)
    var imageView = arrayOfNulls<ImageView>(images.size)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val gridLayout = findViewById<GridLayout>(R.id.layout)

        for (i in images.indices){
            imageView[i] = ImageView(this)
            imageView[i]?.setImageResource(images[i])
            imageView[i]?.setPadding(10,5,2,5)
            val layoutParams = ViewGroup.LayoutParams(500, 500)
            imageView[i]?.layoutParams = layoutParams
            //添加到布局管理器当中
            gridLayout.addView(imageView[i])
        }

    }
}
